package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.constant.CacheConstants;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.entity.BasicSensorUnitEntity;
import com.iwomy.secureplat.platform.service.BasicSensorUnitService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
 * 传感器单位
 *
 * @author pig
 * @date 2024-01-04 18:52:01
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicSensorUnit")
@Tag(description = "basicSensorUnit", name = "传感器单位管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicSensorUnitController {

	private final BasicSensorUnitService basicSensorUnitService;

	/**
	 * 分页查询
	 *
	 * @param page            分页对象
	 * @param basicSensorUnit 传感器单位
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicSensorUnit_view')")
	public R getBasicSensorUnitPage(@ParameterObject Page page, @ParameterObject BasicSensorUnitEntity basicSensorUnit) {
		LambdaQueryWrapper<BasicSensorUnitEntity> wrapper = Wrappers.lambdaQuery(basicSensorUnit);
		return R.ok(basicSensorUnitService.page(page, wrapper));
	}
	/**
	 * 列表
	 *
	 * @param basicSensorUnit 传感器单位
	 * @return
	 */
	@Operation(summary = "分页查询", description = "列表")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_basicSensorUnit_view')")
	public R getBasicSensorUnitList(@ParameterObject BasicSensorUnitEntity basicSensorUnit) {
		LambdaQueryWrapper<BasicSensorUnitEntity> wrapper = Wrappers.lambdaQuery(basicSensorUnit);
		return R.ok(basicSensorUnitService.list( wrapper));
	}


	/**
	 * 通过id查询传感器单位
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicSensorUnit_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicSensorUnitService.getById(id));
	}

	/**
	 * 新增传感器单位
	 *
	 * @param basicSensorUnit 传感器单位
	 * @return R
	 */
	@Operation(summary = "新增传感器单位", description = "新增传感器单位")
	@SysLog("新增传感器单位")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicSensorUnit_add')")
	@CacheEvict(value = CacheConstants.SENSOR_UNIT, allEntries = true)
	public R saveSensorUnit(@RequestBody BasicSensorUnitEntity basicSensorUnit) {
		basicSensorUnit.setSensorUnitCode(basicSensorUnit.getSensorUnitCode());
		return R.ok(basicSensorUnitService.save(basicSensorUnit));
	}

	/**
	 * 修改传感器单位
	 *
	 * @param basicSensorUnit 传感器单位
	 * @return R
	 */
	@Operation(summary = "修改传感器单位", description = "修改传感器单位")
	@SysLog("修改传感器单位")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicSensorUnit_edit')")
	@CacheEvict(value = CacheConstants.SENSOR_UNIT, allEntries = true)
	public R updateSensorUnitById(@RequestBody BasicSensorUnitEntity basicSensorUnit) {
		basicSensorUnit.setSensorUnitCode(basicSensorUnit.getSensorUnitCode());
		return R.ok(basicSensorUnitService.updateById(basicSensorUnit));
	}

	/**
	 * 通过id删除传感器单位
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除传感器单位", description = "通过id删除传感器单位")
	@SysLog("通过id删除传感器单位")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicSensorUnit_del')")
	@CacheEvict(value = CacheConstants.SENSOR_UNIT, allEntries = true)
	public R removeSensorUnitById(@RequestBody Long[] ids) {
		return R.ok(basicSensorUnitService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicSensorUnit 查询条件
	 * @param ids             导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicSensorUnit_export')")
	public List<BasicSensorUnitEntity> export(BasicSensorUnitEntity basicSensorUnit, Long[] ids) {
		return basicSensorUnitService.list(Wrappers.lambdaQuery(basicSensorUnit).in(ArrayUtil.isNotEmpty(ids), BasicSensorUnitEntity::getId, ids));
	}
}